﻿
using System.Globalization;

namespace Common
{
    /// <summary>
    /// 将数字转化为汉字
    /// </summary>
    public static class NumberParse
    {
        /// <summary>   
        /// 转换数字   
        /// </summary>   
        /// <param name="str">转换的字符串（二位以内）</param>   
        /// <returns></returns>  
        public static string Convertdigit(string str)
        {
            int strlen = str.Length;
            string rstr = "";
            switch (strlen)
            {
                case 1: rstr = ConvertChinese(str); break;
                case 2: rstr = Convert2Digit(str); break;
            }
            strlen = rstr.Length;
            return rstr;
        }
        /// <summary>   
        /// 转换二位数字   
        /// </summary>   
        /// <param name="str"></param>   
        /// <returns></returns>
        private static string Convert2Digit(string str)
        {
            string str1 = str.Substring(0, 1);
            string str2 = str.Substring(1, 1);
            string rstring = "";
            if (str1 == "1")//如果十位是1的话，不需要输出一，而只需输出十
                rstring += "十";
            else
                rstring += ConvertChinese(str1) + "十";
            rstring += ConvertChinese(str2);
            return rstring;
        }

        /// <summary>   
        /// 将一位数字转换成中文大写数字   
        /// </summary>   
        /// <param name="str"></param>   
        /// <returns></returns>   
        private static string ConvertChinese(string str)
        {
            //"一二三四五六七八九十"   
            string cstr = "";
            switch (str)
            {
                case "1": cstr = "一"; break;
                case "2": cstr = "二"; break;
                case "3": cstr = "三"; break;
                case "4": cstr = "四"; break;
                case "5": cstr = "五"; break;
                case "6": cstr = "六"; break;
                case "7": cstr = "七"; break;
                case "8": cstr = "八"; break;
                case "9": cstr = "九"; break;
            }
            return (cstr);
        }

        public static bool ContainsDigital(this string str)
        {
            for (var i = 0; i < 10; i++)
            {
                if (str.Contains(i.ToString(CultureInfo.InvariantCulture)))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
